#include<windows.h>
#include<iostream>
#include<fstream>

using namespace std;
typedef char strings;



LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ConFigWindow(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
    static TCHAR szAppName[] = L"MyWindowsApp";
    HWND  hwnd;
    MSG   msg;
    WNDCLASS wdcs;
	strings config_text;
	ifstream config;

	config.open("config.txt", ios::in);
	
	config >> config_text;

	config.close();

    wdcs.style = CS_HREDRAW|CS_VREDRAW;
    wdcs.lpszClassName = szAppName;
    wdcs.lpszMenuName = NULL;
    if(config_text == '1') {
		wdcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	}
	else if(config_text == '2')
	{
		wdcs.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	}
	else if(config_text == '3')
	{
		wdcs.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
	}
	else if(config_text == '4')
	{
		wdcs.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	}
	else if(config_text == '5')
	{
		wdcs.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	}
	else
	{
		wdcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	}
    wdcs.lpfnWndProc = WinProc;
    wdcs.cbClsExtra = 0;
    wdcs.cbWndExtra = 0;
    wdcs.hInstance = hInstance;
    wdcs.hIcon = LoadIcon( NULL, IDC_ARROW );
    wdcs.hCursor = LoadCursor( NULL, IDI_APPLICATION );
    
    if( !RegisterClass( &wdcs ) )
    {
        return 0;
    }

    hwnd = CreateWindow(
                szAppName,
                TEXT("Rizoti Dtryil Code"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                NULL,
                NULL,
                hInstance,
                NULL
    );
    ShowWindow( hwnd, nCmdShow );
    UpdateWindow( hwnd );

    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    return msg.wParam;
};

LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC          hdc;
    PAINTSTRUCT ps;
    RECT        rect;
    static HWND hwndbtn1;
    static HWND hwndbtn2;
    static HWND hwndbtn3;
    static HWND hwndInput;

    switch( message )
    {
        case WM_CREATE:
            hwndbtn1 = CreateWindow(
                                TEXT("button"),
                                TEXT("退出"),
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                150, 500,
                                100, 30,
                                hwnd,
                                (HMENU)1,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL
            );
            hwndbtn2 = CreateWindow(
                                TEXT("button"),
                                TEXT("保存"),
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                400, 500,
                                100, 30,
                                hwnd,
                                (HMENU)2,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL
            );
            hwndbtn3 = CreateWindow(
                                TEXT("button"),
                                TEXT("编译"),
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                600, 500,
                                100, 30,
                                hwnd,
                                (HMENU)3,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL
            );
            hwndInput = CreateWindow(
                                TEXT("edit"),
                                NULL,
                                WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|WS_BORDER|ES_LEFT|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,
                                10, 10,
                                1200, 400,
                                hwnd,
                                (HMENU)4,
                                ((LPCREATESTRUCT) lParam) -> hInstance,
                                NULL
            );
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case 1:
                    switch(HIWORD(wParam))
                    {
                        case BN_CLICKED:
                            PostQuitMessage(0);
                            return 0;
                    }
                    return 0;
                case 2:
                    switch(HIWORD(wParam))
                    {
                        case BN_CLICKED:
                            WCHAR a[9024];
                            GetDlgItemText(hwnd, 4, a, 9024);
                            FILE *fp;

                            fp = fopen("code.c", "w");
                            fprintf(fp, (const char *)a);
                            fclose(fp);
                            return 0;
                    }
                    return 0;
                case 3:
                    switch(HIWORD(wParam))
                    {
                        case BN_CLICKED:
                            WCHAR a[9024];
                            GetDlgItemText(hwnd, 4, a, 9024); //因怕内存溢出 所以才设了9kb
                            FILE *fp;

                            fp = fopen("code.c", "w");
                            fprintf(fp, (const char *)a);
                            fclose(fp);
                            Sleep(3000);
                            system("gcc code.c -o code");
                            Sleep(2000);
                            system("start code.exe");
                            return 0;
                    }
                    return 0;
                case 4:
                    return 0;
            }
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}